package org.cladwell.Handler;

import org.cladwell.domain.JsonResponce;
import org.cladwell.exception.ConditionException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class CommonGlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonResponce<String> commonExceptionHandler(HttpServletRequest request, Exception e) {
        String errorMessage = e.getMessage();
        if(e instanceof ConditionException){
            String errorcode=((ConditionException) e).getCode();
            return new JsonResponce<>(errorcode,errorMessage);
        }else {
            return new JsonResponce<>("500","erroMsg");
        }

    }
}
